package com.aaa.controller.systemmanage;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.LogMonitor;
import com.aaa.controller.BaseController;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.Result;
import com.aaa.entity.systemmanage.Role;
import com.aaa.service.systemmanage.RoleMenuService;
import com.aaa.service.systemmanage.RoleService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2022-08-31 16:00:14
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    //查询所有
    @RequestMapping(method = RequestMethod.GET, value = "/queryAll.do")
    public Result queryAll(PageSortEntity pageSortEntity, Role role) {
        Page page = roleService.queryAll(pageSortEntity, role);
        return success(page.getResult(), page.getTotal());
    }

    //为下拉框查询所有
    @RequestMapping(method = RequestMethod.POST, value = "/queryAllToSelect.do")
    public Result queryAllToSelect() {
        List<Role> list = roleService.queryAllToSelect();
        return success(list);
    }

    //添加角色
    @PostMapping("insertRole.do")
    public Result insert(Role role, String menus) {
        Role insert = roleService.insert(role);
        if (ObjectUtil.isNotEmpty(insert.getRoleId())) {
            String[] menusId = menus.split(",");
            roleMenuService.insert(insert.getRoleId(), menusId);
            return success();
        }
        return fail();
    }

    //删除角色
    @LogMonitor
    @RequestMapping("/delete.do")
    public Result delete(String[] ids) {
        List list = new ArrayList();
        for (String id : ids) {
            list.add(id);
        }
        boolean user1 = roleService.delete(list);
        if (user1) {
            return success();
        } else {
            return fail();
        }
    }

    //修改用户
    @RequestMapping("/update.do")
    public Result update(Role role, String menus) {
        Role update = roleService.update(role);
        if (update != null) {
            String[] menusId = menus.split(",");
            roleMenuService.update(update.getRoleId(), menusId);
            return success();
        }
        return fail();
    }


}

